package com.aurora.gulimall.order.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.aurora.gulimall.order.entity.MqMessageEntity;
import com.aurora.gulimall.order.service.MqMessageService;
import com.aurora.common.utils.PageUtils;
import com.aurora.common.utils.R;



/**
 * 
 *
 * @author aurora
 * @email 244889396@qq.com
 * @date 2022-09-29 13:40:54
 */
@RestController
@RequestMapping("order/mqmessage")
public class MqMessageController {
    @Autowired
    private MqMessageService mqMessageService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = mqMessageService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{messageId}")
    public R info(@PathVariable("messageId") String messageId){
		MqMessageEntity mqMessage = mqMessageService.getById(messageId);

        return R.ok().put("mqMessage", mqMessage);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MqMessageEntity mqMessage){
		mqMessageService.save(mqMessage);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MqMessageEntity mqMessage){
		mqMessageService.updateById(mqMessage);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] messageIds){
		mqMessageService.removeByIds(Arrays.asList(messageIds));

        return R.ok();
    }

    /**
     * 监听消息测试
     * queues： 声明需要监听的所有队列
     */
//    @RabbitListener(queues = {"hello-java-queue"})
    @RabbitHandler
    public void receiveMessage(Message message, Channel channel){
        byte[] body = message.getBody();
        //消息头属性信息
        MessageProperties messageProperties = message.getMessageProperties();
        //deliveryTag: channel内自增
        long deliveryTag = messageProperties.getDeliveryTag();
        //消息应答 multiple:false非批量模式
        try {
            channel.basicAck(deliveryTag,false);
            //long deliveryTag, boolean multiple, boolean requeue
            channel.basicNack(deliveryTag,false,true);
            //long deliveryTag, boolean requeue
//            channel.basicReject();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println("接收的消息为："+message);
    }


}
